﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fastyou.WCF.Proxys;
using System.ServiceModel;
using Fastyou.Model;
using System.ServiceModel.Channels;

namespace Fastyou.WCF.Client.Consoles
{
    class Program
    {
        static void Main(string[] args)
        {
            //Event();
            HelloWorld();
            ////Session();
            ////WCFContract();
            //InsstanceMode();
            Console.Read();

        }

        static void InsstanceMode()
        {
            try
            {
                InstanceModeClient fyfault = new InstanceModeClient("NETHttpBinding_IInstanceMode");

                fyfault.Release(2);
                fyfault.Release(2);

                fyfault = new InstanceModeClient("SessionNETHttpBinding_IInstanceMode");

                fyfault.Release(3);
                fyfault.Release(3);

                fyfault = new InstanceModeClient("SingleNETHttpBinding_IInstanceMode");

                fyfault.Release(5);
                fyfault.Release(5);

                fyfault.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.Read();
            }
        }
        //IDuplex server;

        static void Event()
        {
            //InstanceContext instanceContext = new InstanceContext(this);
            DuplexCallback callback = new DuplexCallback();
            IDuplex server = new DuplexClient(new System.ServiceModel.InstanceContext(callback), "NetTcpBinding_IDuplex");

            //server = new DuplexClient(instanceContext, "WSDualHttpBinding_IDuplex");
            server.Start();
            Work job = new Work();
            job.WorkName = "sky";
            
            job.status = "ok";
            server.AddJob(job);
            server.DoWork("sky");
            //LoadJobs();
        }
        static void HelloWorld()
        {
            try
            {
                Console.WriteLine("-----------------------request begin-----------------------");
                #region code for client;
                BasicHttpBinding bind = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress("http://localhost:8087/hello");

                Fastyou.WCF.Client.Consoles.ServiceReference1.HelloClient ws = new Fastyou.WCF.Client.Consoles.ServiceReference1.HelloClient(bind, address);
                ws.SayHelloCompleted += new EventHandler<Fastyou.WCF.Client.Consoles.ServiceReference1.SayHelloCompletedEventArgs>(ws_SayHelloCompleted);
                ws.SayHelloAsync("sky.lai");
                //ws.Close();
                #endregion

                #region configuration for client

                //HelloClient hc = new HelloClient("BasicHttpBinding_IHello");
                //hc.SayHello("I'm fastyou. \r\n");
                //hc.Close();

                //RequestReplyClient reply = new RequestReplyClient("BasicHttpBinding_IRequestReply");
                //reply.NoneReturnValue();
                //Console.WriteLine(reply.GetReturnValue("sky"));
                //reply.Close();

                //IsOneWayClient one = new IsOneWayClient("BasicHttpBinding_IIsOneWay");
                //Console.WriteLine(DateTime.Now.ToString());
                //one.SetOneWay();
                //Console.WriteLine(DateTime.Now.ToString());
                //one.Close();


                //try
                //{
                //    DuplexCallback callback = new DuplexCallback();
                //    IDuplex wss = new DuplexClient(new System.ServiceModel.InstanceContext(callback), "WSDualHttpBinding_IDuplex");
                //    Console.WriteLine("--------------------Duplex Calls ---------------------------");
                //    Console.WriteLine("开始调用服务");
                //    Console.WriteLine("收到返回信息：" + wss.DoWork("duplex job"));
                //    Console.WriteLine("-------------------------------------------------------------");
                //    Console.WriteLine("\r\n\r\n\r\n");
                //}
                //catch (Exception ex)
                //{
                //    Console.WriteLine(ex.Message);
                //}

                #endregion

                Console.WriteLine("-------------------------request end------------------------------------");
                Console.WriteLine("\r\n");


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.Read();
            }

            Console.Read();
        }

        static void ws_SayHelloCompleted(object sender, Fastyou.WCF.Client.Consoles.ServiceReference1.SayHelloCompletedEventArgs e)
        {

            Console.WriteLine(e.Result);
        }

        static void WCFContract()
        {
            try
            {
                //FYServiceContractClient fyservice = new FYServiceContractClient("BasicHttpBinding_IFYServiceContract");

                //FYDataContract fy = new FYDataContract() { Email = "fastyou.lai@gmail.com", FirstName = "sky", Salary = 3000m };

                //fyservice.PayMoney(fy);
                //fyservice.Close();
                //}
                //catch (Exception ex)
                //{
                //    Console.WriteLine(ex.Message);
                //    Console.Read();
                //}
                FYFaultContractsClient fyfault = new FYFaultContractsClient("BasicHttpBinding_IFYFaultContracts");
                try
                {
                    fyfault.DealFault(1, 0);
                }
                catch (FaultException<ErrorMessage> ex)
                {
                    ErrorMessage error = ex.Detail;
                    Console.WriteLine("An Fault is thrown.\n\tFault code:{0}\n\tFault Reason:{1}\n\tOperation:{2}\n\tMessage:{3}", ex.Code, ex.Reason, error.Operation, error.Messages);
                }
                fyfault.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.Read();
            }

        }
        static void Session()
        {
            try
            {
                //         ChannelFactory<INoneSession> factory = new ChannelFactory<INoneSession>(new BasicHttpBinding(),
                //"http://localhost:8089/ServiceSession/NoneSession");
                //         factory.CreateChannel().Add(1F);

                //IBasicHttpSession proxy1 = cf.CreateChannel();
                //Console.WriteLine("begin invoke add(1,2)");
                //proxy1.Add(1, 2);
                //Console.WriteLine("begin invoke add(3,4)");
                //proxy1.Add(3, 4);
                //Console.WriteLine("the result is {0}", proxy1.GetResult());

                //IBasicHttpSession proxy2 = cf.CreateChannel();
                //Console.WriteLine("begin invoke add(5,6)");
                //proxy2.Add(5, 6);
                //Console.WriteLine("begin invoke add(7,8)");
                //proxy2.Add(7, 8);
                //Console.WriteLine("the result is {0}", proxy2.GetResult());
                //BasicHttpBinding bind = new BasicHttpBinding();
                //EndpointAddress address = new EndpointAddress("http://localhost:8086/ServiceSession/BasicHttpSession");

                //IBasicHttpSession ws = new BasicHttpSessionClient(bind, address);
                //ws.Add(4, 5);
                //BasicHttpSessionsClient bhsc = new BasicHttpSessionsClient("BasicHttpBinding_IBasicHttpSessions");
                //bhsc.Add(4, 5);
                //Console.WriteLine("the result is {0}", bhsc.GetResult());
                //bhsc.Close();\


                //ChannelFactory<IRequestChannel> ch = bind.BuildChannelFactory<IRequestChannel>;
                //IRequestChannel rc = ch.CreateChannel();


                ChannelFactory<INoneSession> cf = new ChannelFactory<INoneSession>("BasicHttpBinding_INoneSession");
                INoneSession proxy1 = cf.CreateChannel();

                Console.WriteLine("begin add 1");

                proxy1.Add(1);
                Console.WriteLine("begin add 2");

                proxy1.Add(2);

                Console.WriteLine("begin getresult");
                Console.WriteLine("the result is {0}", proxy1.getResult());
                Console.WriteLine("end getresult");
                //(proxy1 as ICommunicationObject).Close();
                //try
                //{
                //    proxy1.Add(3);
                //}
                //catch (Exception ex)
                //{
                //    Console.WriteLine(ex.Message);
                //}
                INoneSession proxy2 = cf.CreateChannel();

                Console.WriteLine("begin add 1");

                proxy2.Add(1);
                Console.WriteLine("begin add 2");

                proxy2.Add(2);

                Console.WriteLine("begin getresult");
                Console.WriteLine("the result is {0}", proxy2.getResult());
                Console.WriteLine("end getresult");
                cf.Close();
                //ab.Close();
                //(proxy1 as ICommunicationObject).Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.Read();
            }

        }
    }
}